package com.java.base.io.enter;

import java.io.Console;
import java.util.Scanner;

public class ScannerTest {


    public static void main(String[] args) throws InterruptedException {

        String s = "abc 测试 123";

        String t = "type " + s;
        for (int type = 0; type <= 8; type++) {
            System.out.println(colorFont(t, type, null, null));

        }

        String f = "fore " + s;

        for (int fore = 0; fore <= 7; fore++) {
            System.out.println(colorFont(f, null, fore, null));
            System.out.println(colorFont(f, 1, fore, null));
        }

        String b = "back " + s;
        for (int back = 0; back <= 7; back++) {
            System.out.println(colorFont(b, null, null, back));
            System.out.println(colorFont(b, 5, null, back));
        }

    }


    public static String colorFont(String s, Integer type, Integer foreground, Integer background) {
        StringBuilder sb = new StringBuilder(PREFIX);
        if (type != null) {
            sb.append(type);
        }

        if (foreground != null) {
            if (sb.charAt(sb.length() - 1) != '[') {
                sb.append(';');
            }
            sb.append(FOREGROUND + foreground);
        }

        if (background != null) {
            if (sb.charAt(sb.length() - 1) != '[') {
                sb.append(';');
            }
            sb.append(BACKGROUND + background);
        }

        return sb.append('m').append(s).append(SUFFIX).toString();
    }


    static String PREFIX = "\033[";
    static String SUFFIX = PREFIX + "0m";

    static int TYPE_DEFAULT = 0;
    static int TYPE_HIGH_LIGHT = 1;
    static int TYPE_VAGUE = 2;
    static int TYPE_ITALIC = 3;
    static int TYPE_UNDERLINE = 4;
    static int TYPE_TWINKLE_SLOW = 5;
    static int TYPE_TWINKLE_FAST = 6;
    static int TYPE_CHANGE_COLOR = 7;
    static int TYPE_HIDE = 8;

    static int FOREGROUND = 30;
    static int BACKGROUND = 40;


    static int COLOR_BLACK = 0;
    static int COLOR_RED = 1;
    static int COLOR_GREEN = 2;
    static int COLOR_YELLOW = 3;
    static int COLOR_BLUE = 4;
    static int COLOR_VIOLET = 5;
    static int COLOR_CYAN = 6;
    static int COLOR_WHITE = 7;


    public static void consoleReadLine() throws InterruptedException {
        Console console = System.console();
        if (console == null) {
            System.out.println("Console is null");
            System.exit(0);
        }

        String s = console.readLine("bash>");
        console.printf(s);
        Thread.sleep(1000);
        console.printf("\r\n");

    }

    public static void password() {
        Console console = System.console();
        if (console == null) {
            System.out.println("Console is null");
            System.exit(0);
        }

        while (true) {
            console.printf("start...");
            char[] cs = console.readPassword("Enter Password:");
            String pwd = new String(cs) + "\r\n";
            console.printf(pwd);
            if ("exit".equals(pwd)) {
                break;
            }
        }

    }

    public static void scanner() {
        Scanner scanner = new Scanner(System.in);
        System.out.print(">");
        String s = scanner.next();
        System.out.println(s);
    }


}
